import { getRandom, random } from "src/utils";
import * as PIXI from "pixi.js";
import anime from "animejs/lib/anime.es.js";

export function drawStar(app: PIXI.Application) {
  const color = getRandom();
  const containers: PIXI.Container = new PIXI.Container();
  const size = random(0.5, 1);
  const scale = random(0.75, 2);
  const ran = random(0, 1);
  const flag = ran < 0.5;
  const stars: Array<PIXI.Graphics> = [];
  function riStars(R: number, num: number) {
    const Bpoints = generatePolygonPoints(R, 5, 0, 0);
    const LitBpoints = flag
      ? generatePolygonPoints(R * num, 5, 0, 0)
      : undefined;
    const bezier = new PIXI.Graphics();
    bezier.beginFill(color, 1);
    bezier.lineStyle(0, color, 1);
    bezier.moveTo(Bpoints[0].x, Bpoints[0].y);
    // 绘制
    bezierStars(bezier, Bpoints, LitBpoints);
    bezier.endFill();

    containers.addChild(bezier);
    stars.push(bezier)
  }
  let SNum = flag ? 300 : 100;
  riStars(SNum * 0.9 * size * radVNums(), 0.6);
  riStars(SNum * size * radVNums(), 0.95);
  app.stage.addChild(containers);
  containers.position.x = random(0, window.innerWidth);
  containers.position.y = random(0, window.innerHeight);
  containers.scale.set(0);
  containers.rotation = random(0, 6.28);
  anime({
    targets: containers.scale,
    keyframes: [
      {
        x: scale * 0.25,
        y: scale * 0.25,
        duration: 800,
        delay: 60,
      },
      {
        x: 0,
        y: 0,
        duration: 600,
        delay: 60,
        easing: "easeInBack",
      },
    ],
    complete: function () {
      stars[0].removeChild();
      stars[1].removeChild();
      containers.removeAllListeners();
      app.stage.removeChild(containers);
    },
  });
}
export function drawStars(app: PIXI.Application,options:{min:number,max:number} ) {
  for(let i = 0; i < random(options.min,options.max) ; i++){
    drawStar(app)
  }
}

export const bezierStars = (
  bezier: PIXI.Graphics,
  Bpoints: Array<PIXI.Point>,
  LitBpoints?: Array<PIXI.Point>
) => {
  for (let i = 0; i < 5; i++) {
    let j = i + 1;
    if (j > 4) j = 0;
    let as = (Bpoints[i].x + Bpoints[j].x) * 1.5;
    let bs = (Bpoints[i].y + Bpoints[j].y) * 1.5;
    bezier.bezierCurveTo(
      Bpoints[i].x,
      Bpoints[i].y,
      as,
      bs,
      Bpoints[j].x,
      Bpoints[j].y
    );
  }
  if (LitBpoints) {
    bezier.beginHole();
    for (let i = 0; i < 5; i++) {
      let j = i + 1;
      if (j > 4) j = 0;
      let as = (LitBpoints[i].x + LitBpoints[j].x) * 1.5;
      let bs = (LitBpoints[i].y + LitBpoints[j].y) * 1.5;
      bezier.bezierCurveTo(
        LitBpoints[i].x,
        LitBpoints[i].y,
        as,
        bs,
        LitBpoints[j].x,
        LitBpoints[j].y
      );
    }
    bezier.bezierCurveTo(
      LitBpoints[0].x,
      LitBpoints[0].y,
      (LitBpoints[0].x + LitBpoints[1].x) * 1.5,
      (LitBpoints[0].y + LitBpoints[1].y) * 1.5,
      LitBpoints[1].x,
      LitBpoints[1].y
    );

    bezier.endHole();
  }
};

export function generatePolygonPoints(
  radius: number,
  num: number,
  X: number,
  Y: number
): PIXI.Point[] {
  let deltaRad = (Math.PI * 2) / num,
    points: PIXI.Point[] = [];
  for (let index = 0; index < num; index++) {
    let x = Math.sin(index * deltaRad) * radius + X,
      y = Math.cos(index * deltaRad) * radius + Y;
    points.push(new PIXI.Point(x, y));
  }
  return points;
}

export function radVNums(): number {
  return (
    Math.sqrt(
      Math.pow(window.innerHeight, 2) + Math.pow(window.innerWidth, 2)
    ) / 2136
  );
}
